import router, { replaceChildRoutesDirectly } from "../router";
import useOperationStore from "../store/operation";
import eventBus from "../eventBus";
import { RouterLink } from "vue-router";
const modules = import.meta.glob('@/pages/**/*.vue');

const loadView = (path) => {
  return () => import(/* @vite-ignore */ path)
}

export function getRouteList() {
  const routeList = [];
  router.options.routes.forEach((route) => {
    if (route.children) {
      routeList.push(...route.children);
    } else {
      routeList.push(route);
    }
  });
  return routeList;
}

function traverseObject(obj,fn){  
  for(let key in obj){  
    if(key === 'children'){            
      for(let item of obj.children){
        // console.log(item);  
        traverseObject(item,fn)
      }
    }
    else if(key === 'component'){
      fn(obj)
    }
  }
}
export function replaceRoute() {
  console.log('replaceRoute');
  
  const operationStore = useOperationStore();

  const routerStr = operationStore.data[operationStore.index].router  
  const routerJson = JSON.parse(routerStr)
  for(let route of routerJson){
    traverseObject(
      route,(obj)=>{
        let path = obj.component
        path = path.replace('@','/src')
        obj.component = modules[path]
      }
    )
  }
  router.removeRoute('Business')
  router.addRoute('Business',{
    path: '/business',
    name: 'Business',
    component: () => import("@/pages/Business/index.vue"),
    children:routerJson
  })
  // const routeList = router.options.routes
  // console.log(routeList)
  eventBus.emit('updateBusinessRoutes')
}


